﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Configuration;
using VIB.Model;
using System.Text.RegularExpressions;
using System.IO.Ports;


namespace VIB.View
{
    /// <summary>
    /// Interaction logic for AddBookWindow.xaml
    /// </summary>
    public partial class AddBook : Window
    {
        private Book currentBook;

        public AddBook()
        {
            InitializeComponent();
            Scanner.Instance.Port.DataReceived += new System.IO.Ports.SerialDataReceivedEventHandler(Port_DataReceived);
        }

        private void Port_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
        {
            SerialPort com = (SerialPort)sender;

            string input = com.ReadExisting();
            input = input.Replace("\r\n", "");
            input = input.Trim();

            Dispatcher.BeginInvoke(new Action(() => textbox_isbn.Text += input));
        }

        public void RefreshControlStatus(object sender, TextChangedEventArgs e)
        {
          
            if (textbox_isbn.Text != String.Empty && Regex.IsMatch(textbox_isbn.Text, @"^(97(8|9))?\d{9}(\d|X)$"))
            {
                button_loadBookInfo.IsEnabled = true;
                textbox_author.IsEnabled = true;
                textblock_error.Text = String.Empty;
            }
            else
            {
                button_loadBookInfo.IsEnabled = false;
                textbox_author.IsEnabled = false;
                textblock_error.Text = "Keine gültige ISBN Nummer";
            }

            if (textbox_author.Text != String.Empty)
            {
                textbox_title.IsEnabled = true;
            }
            else
            {
                textbox_title.IsEnabled = false;
            }

            if (textbox_title.Text != String.Empty)
            {
                textbox_year.IsEnabled = true;
            }
            else
            {
                textbox_year.IsEnabled = false;
            }

            int temp;
            if (textbox_isbn.Text != String.Empty && textbox_author.Text != String.Empty && textbox_title.Text != String.Empty && Int32.TryParse(textbox_year.Text, out temp))
            {
                button_saveBook.IsEnabled = true;
            }
            else
            {
                button_saveBook.IsEnabled = false;
            }
        }

        private void SaveBook(object sender, RoutedEventArgs e)
        {
            int year = 0;
            Int32.TryParse(textbox_year.Text, out year);
            
            currentBook = new Book(textbox_title.Text, textbox_author.Text, year, textbox_isbn.Text);

            if (currentBook.Author != String.Empty && currentBook.Title != String.Empty && currentBook.Isbn != String.Empty && currentBook.Year > 0)
            {
                try
                {
                    DataManager.AddBook(currentBook,false);
                }
                catch (Exception ex)
                {
                    if (ex.Message == "BookAlreadyInDataBase")
                    {
                        if (MessageBox.Show("Buch ist bereits in der Datenbank vorhanden, zusätzliches Exemplar hinzufügen?",
                            "Bereits vorhanden", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
                        {
                            DataManager.AddBook(currentBook, true);
                        }
                    }
                }                
            }
            else
            {
                MessageBox.Show("Ungültige Daten");
            }           
            this.Close();
        }

        private void LoadBookInfo(object sender, RoutedEventArgs e)
        {
            try
            {
                currentBook = OpenLibrary.getBookInfo(textbox_isbn.Text);
                if (currentBook != null)
                {
                    textbox_isbn.Text = currentBook.Isbn;
                    textbox_title.Text = currentBook.Title;
                    textbox_author.Text = currentBook.Author;
                    textbox_year.Text = currentBook.Year.ToString();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }
    }
}
